package classes;


import db.DriverDAO;
import db.UserDAO;
import java.util.List;


public class User {

    private int id;
    private String username;
    private String password;
    private int permissionLevelID;
    private int driverID;
    private String permissionLevelDesc;
    private String driverName;
    private static User editableUser;
    private static User loggedInUser;

    //<editor-fold desc="Constructors">
    public User() {
    }

    public User(int id, String username, String password, int permissionLevelID, int driverID,
            String permissionLevelDesc, String driverName) {
        this.id = id;
        this.username = username;
        this.password = password;
        this.permissionLevelID = permissionLevelID;
        this.driverID = driverID;
        this.permissionLevelDesc = permissionLevelDesc;
        this.driverName = driverName;
    }
    //</editor-fold>

    //<editor-fold desc="Getters and Setters">
    public int getId() {
        return id;
    }

    public int getDriverID() {
        return driverID;
    }

    public void setDriverID(int driverID) {
        this.driverID = driverID;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public int getPermissionLevelID() {
        return permissionLevelID;
    }

    public void setPermissionLevelID(int permissionLevelID) {
        this.permissionLevelID = permissionLevelID;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getDriverName() {
        return driverName;
    }

    public void setDriverName(String driverName) {
        this.driverName = driverName;
    }

    public static User getLoggedInUser() {
        return loggedInUser;
    }

    public static void setLoggedInUser(User loggedInUser) {
        User.loggedInUser = loggedInUser;
    }

    public String getPermissionLevelDesc() {
        return permissionLevelDesc;
    }

    public void setPermissionLevelDesc(String permissionLevelDesc) {
        this.permissionLevelDesc = permissionLevelDesc;
    }
    //</editor-fold>

    //<editor-fold desc="Editable Driver">
    public void setEditableUser(User user) {
        editableUser = user;
    }

    public User getEditableUser() {
        return editableUser;
    }

    public String addUser() {
        if (UserDAO.addUser(this)) {
            return "userList";
        } else {
            return "userAddFailure";
        }
    }

    public String deleteUser() {
        if (UserDAO.deleteUser(this)) {
            return "userList";
        } else {
            return "userDeleteFailure";
        }
    }

    public List<User> getUsers() {
        return UserDAO.getUsers();
    }

    public String saveUser() {
        if (UserDAO.saveUser(this)) {
            editableUser = null;
            return "userList";
        } else {
            return "userSaveFailure";
        }
    }
    //</editor-fold>

    public String login() {

        User checkUser = UserDAO.getUserByUserName(this.getUsername());

        if (checkUser.getPassword().equals(this.getPassword())) {
            loggedInUser = checkUser;
            if (checkUser.getDriverID() == 0 && checkUser.getPermissionLevelID() != 1) {
                return "addDriver";
            } else {
                return "dbtest";
            }
        } else {
            return "login";
        }
    }

    @Override
    public String toString() {
        return this.username;
    }
}